﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Serialization;

namespace iNet.Media.Imaging
{
	/// <summary>
	/// 表示影像中的區域。
	/// </summary>
	[Serializable]
	public class Region : IXmlSerializable
	{
		#region 靜態欄位
		static readonly Regex _RectangleStringRegex = new Regex(@"^[\s]*(?<X>\-?\d+(\.\d+)?)[\s]*\,[\s]*(?<Y>\-?\d+(\.\d+)?)[\s]*\,[\s]*(?<Width>\-?\d+(\.\d+)?)[\s]*\,[\s]*(?<Height>\-?\d+(\.\d+)?)[\s]*$");
        #endregion
		#region 欄位
		string _Name;
		Rect _Rectangle;
		#endregion
		#region 建構子
		private Region()
		{ }
		/// <summary>
		/// 初始化新的 Region 執行個體。
		/// </summary>
		/// <param name="name">區域的名稱。</param>
		public Region(string name)
			: this(Rect.Empty, name)
		{ }
		/// <summary>
		/// 初始化新的 Region 執行個體。
		/// </summary>
		/// <param name="rect">區域在影像中的矩形範圍。</param>
		public Region(Rect rect)
			: this(rect, null)
		{ }
		/// <summary>
		/// 初始化新的 Region 執行個體。
		/// </summary>
		/// <param name="rect">區域在影像中的矩形範圍。</param>
		/// <param name="name">區域的名稱。</param>
		public Region(Rect rect, string name)
		{
			_Name = name;
			_Rectangle = rect;
		}
		#endregion
		#region IXmlSerializable.GetSchema
		System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
		{
			return null;
		}
		#endregion
		#region IXmlSerializable.ReadXml
		void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
		{
			this.ReadXml(reader);
		}
		#endregion
		#region IXmlSerializable.WriteXml
		void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
		{
			this.WriteXml(writer);
		}
		#endregion
		#region Name
		/// <summary>
		/// 取得區域的名稱。
		/// </summary>
		public string Name
		{
			get
			{
				return _Name;
			}
		}
		#endregion
		#region ReadXml
		/// <summary>
		/// 從 XML 讀取 Region 資訊。
		/// </summary>
		/// <param name="reader">提供 XML 讀取功能的 XmlReader。</param>
		protected virtual void ReadXml(System.Xml.XmlReader reader)
		{
			//確認參數
			if (reader == null)
				throw new ArgumentNullException("reader");

			//讀取名稱
			_Name = reader["Name"];

			//讀取矩形
			string rectString = reader["Rectangle"];
			if (rectString != null)
			{
				Match match = _RectangleStringRegex.Match(rectString);
				if (match.Success)
				{
					double x, y, width, height;
					if (double.TryParse(match.Groups["X"].Value, out x)
						&& double.TryParse(match.Groups["Y"].Value, out y)
						&& double.TryParse(match.Groups["Width"].Value, out width)
						&& double.TryParse(match.Groups["Height"].Value, out height))
					{
						_Rectangle = new Rect(x, y, width, height);
					}
				}
			}
		}
		#endregion
		#region Rectangle
		/// <summary>
		/// 取得區域在影像中的矩形範圍。
		/// </summary>
		public Rect Rectangle
		{
			get
			{
				return _Rectangle;
			}
		}
		#endregion
		#region WriteXml
		/// <summary>
		/// 將 Region 資訊寫入 XML 文件。
		/// </summary>
		/// <param name="writer">提供 XML 寫入功能的 XmlWriter。</param>
		protected virtual void WriteXml(System.Xml.XmlWriter writer)
		{
			if (writer == null)
				throw new ArgumentNullException("writer");
			if (_Name != null)
				writer.WriteAttributeString("Name", _Name);
			writer.WriteAttributeString("Rectangle", string.Format("{0},{1},{2},{3}", _Rectangle.X, _Rectangle.Y, _Rectangle.Width, _Rectangle.Height));
		}
		#endregion
	}
	#region PersonRegion
	/// <summary>
	/// 表示影像中的人員區域。
	/// </summary>
	[Serializable]
	public class PersonRegion : Region
	{
		#region 欄位
		string _EmailAddress;
		#endregion
		#region 建構子
		private PersonRegion()
			: base(Rect.Empty)
		{ }
		/// <summary>
		/// 初始化新的 PersonRegion 執行個體。
		/// </summary>
		/// <param name="rect">人員在影像中的矩形範圍。</param>
		/// <param name="name">人員的名稱。</param>
		/// <param name="emailAddress">人員的 E-mail 位址。</param>
		public PersonRegion(Rect rect, string name, string emailAddress)
			: base(rect, name)
		{
			_EmailAddress = emailAddress;
		}
		#endregion
		#region EmailAddress
		/// <summary>
		/// 取得人員的 E-mail 位址。
		/// </summary>
		public string EmailAddress
		{
			get
			{
				return _EmailAddress;
			}
		}
		#endregion
		#region ReadXml
		/// <summary>
		/// 從 XML 讀取 PersonRegion 資訊。
		/// </summary>
		/// <param name="reader">提供 XML 讀取功能的 XmlReader。</param>
		protected override void ReadXml(System.Xml.XmlReader reader)
		{
			//呼叫基底
			base.ReadXml(reader);

			//讀取 E-mail 位址
			_EmailAddress = reader["EmailAddress"];
		}
        #endregion
		#region WriteXml
		/// <summary>
		/// 將 PersonRegion 資訊寫入 XML 文件。
		/// </summary>
		/// <param name="writer">提供 XML 寫入功能的 XmlWriter。</param>
		protected override void WriteXml(System.Xml.XmlWriter writer)
		{
			base.WriteXml(writer);
			if (_EmailAddress != null)
				writer.WriteAttributeString("EmailAddress", _EmailAddress);
		}
        #endregion
	}
	#endregion
}
